package com.filter;

import java.io.IOException;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpFilter;
/**
 * Servlet Filter implementation class CharacterEncoding
 */
@WebFilter("/CharacterEncoding")
public class CharacterEncoding extends HttpFilter implements Filter {
       
    /**
     * @see HttpFilter#HttpFilter()
     */
	
	protected String encoding=null;
	protected FilterConfig filterConfig=null;
	
    public CharacterEncoding() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		this.encoding=null;
		filterConfig=null;
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
         if(encoding!=null)
         {
        	 request.setCharacterEncoding(encoding);
        	 response.setContentType("text/html;charset="+encoding);
         }
		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		
		this.filterConfig=fConfig;
		this.encoding=fConfig.getInitParameter("encoding");
		
		
		
	}

}
